package com.example.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

  @Bean(name = "redisTemplate")
  RedisTemplate<String, Object> redisTemplate() {
    var template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(new JedisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());

    // valueSerializer对应的是redis数据结构中的String
    template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
    //    var ops = template.boundValueOps("username");
    //    ops.get();
    //    ops.set("zhangSan", 30, TimeUnit.MINUTES);
    return template;
  }
}
